import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [
      {
        path: '/about',
        name: 'About',
        component: () => import('../views/About.vue')
      },
      {
        path: '/log',
        name: 'log',
        component: () => import('../views/log.vue')
      },
      {
        path: '/sta',
        name: 'sta',
        component: () => import('../views/sta.vue')
      },
      {
        path: '/user',
        name: 'user',
        component: () => import('../views/user.vue')
      },
      {
        path: '/userMananger',
        name: 'userManager',
        component: () => import('../views/userManager.vue')
      },
      {
        path: '/role',
        name: 'role',
        component: () => import('../views/Role.vue')
      },{
        path: '/menuManager',
        name: 'menuManager',
        component: () => import('../views/MenuManager.vue')
      },{
        path: '/setting',
        name: 'setting',
        component: () => import('../views/setting.vue')
      },{
        path: '/shops',
        name: 'shops',
        component: () => import('../views/shop/Shops.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/forgetPassword',
    name: 'forgetPassword',
    component: () => import('../views/forgetPassword.vue')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach(function (to, from, next) {
  if (to.name == 'login'|| to.name == 'forgetPassword') {
    next();
  } else {
    if (sessionStorage.getItem("token") != null) {
      next()
    } else {
      next({ name: 'login' })
    }
  }

})

export default router
